package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;
    @RequestMapping(value = "/list",name = "货物下的附件的数据分页查询")
    public String findAll(String contractId, String contractProductId, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        //查询货物下的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);

        PageInfo page = extCproductService.findAll(extCproductExample, pageNum, pageSize);
        request.setAttribute("page", page);


        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);

        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit",name = "货物下附件的新增")
    public String edit(ExtCproduct extCproduct){
        // 设置企业标识
        extCproduct.setCompanyId(companyId);
        // 设置企业名称
        extCproduct.setCompanyName(companyName);

        if(UtilFuns.isEmpty(extCproduct.getId())){

            extCproductService.save(extCproduct);

        }else{

            extCproductService.update(extCproduct);
        }
        // 到最新页面展示最新数据
        return "redirect: /cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    @RequestMapping(value = "/toUpdate", name = "货物下附件编辑页面")
    public String toUpdate(String id, String contractId, String contractProductId) {

        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);



        // 带着合同id和货物id到修改页面
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);
        return "cargo/extc/extc-update";

    }

    @RequestMapping(value = "/delete", name = "货物下附件的删除")
    public String delete(String id, String contractId, String contractProductId) {
        extCproductService.delete(id);
        return "redirect: /cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;

    }

}
